//
//  HomeService.m
//  GC_OC
//
//  Created by  Quan He on 2022/4/8.
//

#import "HomeService.h"
@interface HomeService()
@property (nonatomic, strong) ObjectAFNetWorking* objectAFNet;

@end

@implementation HomeService
- (instancetype)init {
    self = [super init];
    if (self) {
        self.objectAFNet = [[ObjectAFNetWorking alloc]init];
    }
    return self;
}

//首页数据
- (NSDictionary *)getHomeInfo:(NSString *)idString {
    NSString *filePath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/home_id%@_myJson.json",idString]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        NSData* dataString=[NSData dataWithContentsOfFile:filePath];
        NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:dataString options:NSJSONReadingMutableContainers error:nil];
        return dic;
    }
    return [NSDictionary dictionary];
}

- (void)getHomeIdInfoWithIdString:(NSString *)idString finish:(void (^)(NSDictionary *data))success error:(ErrorBlock)errors {
    NSString *filePath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/home_id%@_myJson.json",idString]];
    [self.objectAFNet getUrlStr:[NSString stringWithFormat:@"%@/api/home/getCateData?id=%@",server_addr,idString] finish:^(id  _Nonnull dataString) {
        
        NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:dataString options:NSJSONReadingMutableContainers error:nil];
        [dataString writeToFile:filePath atomically:YES];
        
        success(dic);
    } andError:errors];
}

//热门搜索数据
- (void)requestSearchInfo:(void (^)(NSArray *data))callBack {
    NSMutableArray *searchInfo = [[NSMutableArray alloc] init];
    NSString *filePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/search_Json.json"];
    __weak typeof(self) weakSelf = self;

    [self.objectAFNet getUrlStr:[NSString stringWithFormat:@"%@/api/index/HotSearch",server_addr] finish:^(id  _Nonnull dataString) {
        NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:dataString options:NSJSONReadingMutableContainers error:nil];

        [dataString writeToFile:filePath atomically:YES];

        [searchInfo removeAllObjects];
        [searchInfo addObjectsFromArray:dic[@"data"]];
        callBack(searchInfo);
    } andError:^(NSError * _Nonnull error) {
        callBack([weakSelf getLocalSearchInfo]);
    }];
}

- (NSArray *)getLocalSearchInfo {
    NSMutableArray *searchInfo = [[NSMutableArray alloc] init];
    NSString *filePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/search_Json.json"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        NSData* dataString = [NSData dataWithContentsOfFile:filePath];
        NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:dataString options:NSJSONReadingMutableContainers error:nil];
        [searchInfo addObjectsFromArray:dic[@"data"]];
    }
    if (searchInfo.count == 0) {
        [searchInfo addObject:@"搜索本来很简单"];
    }
    return searchInfo;
}
@end
